package com.cetcs.kmga.dataManager.controller;

import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.common.Page;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.entity.sysCode.vi.SysCodeInfoAddVi;
import com.cetcs.kmga.dataManager.entity.sysCode.vi.SysCodeTypeAddVi;
import com.cetcs.kmga.dataManager.entity.sysCode.vi.SysCodeTypeVi;
import com.cetcs.kmga.dataManager.service.sysCode.SysCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.validation.Valid;

/**
 * Created by Administrator on 2017/11/15 0015.
 */
@RestController
@RequestMapping("/sysCode")
public class SysCodeController extends BaseController {

    @Autowired
    private SysCodeService sysCodeService;

    /**
     * 查询字典类型列表
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "查询字典类型列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getSysCodeTypeList", method = RequestMethod.GET)
    public BaseResponse getSysCodeTypeList(WebRequest request,@RequestParam(required =false,defaultValue ="1")int page,
                                            @RequestParam(required =false,defaultValue ="5")int pageSize) {
        try {
            Data data = getParam(request);
            Data params = new Data();
            params.put("typeName",data.getString("keyValue"));
            params.put("page",page);
            params.put("pageSize",pageSize);
            Page result = sysCodeService.getSysCodeTypeList(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

    /**
     * 查询字典信息列表
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "查询字典信息列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/getSysCodeInfoList", method = RequestMethod.GET)
    public BaseResponse getSysCodeInfoList(WebRequest request,@RequestParam(required =false,defaultValue ="1")int page,
                                           @RequestParam(required =false,defaultValue ="5")int pageSize) {
        try {
            Data data = getParam(request);
            Data params = new Data();
            params.put("value",data.getString("keyValue"));
            params.put("typeId",data.getString("id"));
            params.put("page",page);
            params.put("pageSize",pageSize);
            Page result = sysCodeService.getSysCodeInfoList(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

    /**
     * 添加字典类型
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "添加字典类型", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/addSysCodeType", method = RequestMethod.POST)
    public BaseResponse addSysCodeType(@Valid @RequestBody SysCodeTypeAddVi SysCodeTypeAddVi , BindingResult bindingResult) {
        try {
            Data params = new Data();

            if(SysCodeTypeAddVi.getValueName().length()>50){
                return error("编码类型名称输入长度不能超过50个字符！");
            }

            params.put("typeName",SysCodeTypeAddVi.getValueName());
            int result = sysCodeService.addSysCodeType(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

    /**
     * 添加字典信息
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "添加字典信息", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/addSysCodeInfo", method = RequestMethod.POST)
    public BaseResponse addSysCodeInfo(@RequestBody SysCodeInfoAddVi SysCodeInfoAddVi) {
        try {
            Data params = new Data();

            if(SysCodeInfoAddVi.getValueName().length()>50){
                return error("编码名称输入长度不能超过50个字符！");
            }

            if(SysCodeInfoAddVi.getValCode().length()>30){
                return error("编码code输入长度不能超过30个字符！");
            }

            params.put("typeId",SysCodeInfoAddVi.getId());
            params.put("value",SysCodeInfoAddVi.getValueName());
            params.put("code",SysCodeInfoAddVi.getValCode());
            int result = sysCodeService.addSysCodeInfo(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

    /**
     * 修改字典类型
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "修改字典类型", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/updateSysCodeType", method = RequestMethod.POST)
    public BaseResponse updateSysCodeType(@RequestBody SysCodeTypeVi SysCodeTypeVi) {
        try {

            Data params = new Data();

            if(SysCodeTypeVi.getValue().length()>50){
                return error("编码类型名称输入长度不能超过50个字符！");
            }

            params.put("id",SysCodeTypeVi.getId());
            params.put("typeName",SysCodeTypeVi.getValue());
            int result = sysCodeService.updateSysCodeType(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

    /**
     * 删除字典类型
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "删除字典类型", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/delSysCodeType/{id}", method = RequestMethod.DELETE)
    public BaseResponse delSysCodeType(@PathVariable("id") String id) {
        try {
            Data params = new Data();
            params.put("id",id);
            int result = sysCodeService.delSysCodeType(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

    /**
     * 删除字典信息
     */
    @LogAnnotation(module = "系统管理-码表管理", logType = LogTypeEnum.业务日志, description = "删除字典信息", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/delSysCodeInfo/{id}", method = RequestMethod.DELETE)
    public BaseResponse delSysCodeInfo(@PathVariable("id") String id) {
        try {
            Data params = new Data();
            params.put("id",id);
            int result = sysCodeService.delSysCodeInfo(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(500);
        }
    }

}
